/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.web.jndi_config;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.util.Properties;

/**
 * a fake JDBC {@link Driver} that allows to transmit information from the
 * application server to the application.
 */
public class StringContainerJdbcDriver
    implements Driver
{

  static {
    try {
      // register the driver.
      DriverManager.registerDriver(new StringContainerJdbcDriver());
    }
    catch (Exception e) {
      // Nothing to do there...
    }
  }



  /**
   * Retrieves whether the driver thinks that it can open a connection to the
   * given URL. Typically drivers will return <code>true</code> if they
   * understand the subprotocol specified in the URL and <code>false</code> if
   * they do not.
   * 
   * @param url
   *          the URL of the database
   * @return <code>true</code> if this driver understands the given URL;
   *         <code>false</code> otherwise
   * @see java.sql.Driver#acceptsURL(java.lang.String)
   */
  public boolean acceptsURL(String url)
  {
    return url.startsWith("jdbc:stringContainer:");
  }



  /**
   * Attempts to make a database connection to the given URL. The driver should
   * return "null" if it realizes it is the wrong kind of driver to connect to
   * the given URL. This will be common, as when the JDBC driver manager is
   * asked to connect to a given URL it passes the URL to each loaded driver in
   * turn.
   * <P>
   * The driver should throw an <code>SQLException</code> if it is the right
   * driver to connect to the given URL but has trouble connecting to the
   * database.
   * <P>
   * The <code>java.util.Properties</code> argument can be used to pass
   * arbitrary string tag/value pairs as connection arguments. Normally at least
   * "user" and "password" properties should be included in the
   * <code>Properties</code> object.
   * 
   * @param url
   *          the URL of the database to which to connect
   * @param info
   *          a list of arbitrary string tag/value pairs as connection
   *          arguments. Normally at least a "user" and "password" property
   *          should be included.
   * @return a <code>Connection</code> object that represents a connection to
   *         the URL
   * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
   */
  public Connection connect(String url, Properties info)
  {
    if(!acceptsURL(url)) {
      return null;
    }
    Properties newProperties = new Properties(info);
    newProperties.put("url", url);
    String[] urlParts = url.split(":");
    if (urlParts.length == 3) {
      String[] properties = urlParts[2].split(";");
      for (String property : properties) {
        int index = property.indexOf('=');
        if (index != -1) {
          newProperties.put(property.substring(0, index), property.substring(index + 1));
        }
      }
    }
    return new StringContainerConnection(newProperties);
  }



  /**
   * Retrieves the driver's major version number. Initially this should be 1.
   * 
   * @return this driver's major version number
   * @see java.sql.Driver#getMajorVersion()
   */
  public int getMajorVersion()
  {
    return 1;
  }



  /**
   * Gets the driver's minor version number. Initially this should be 0.
   * 
   * @return this driver's minor version number
   * @see java.sql.Driver#getMinorVersion()
   */
  public int getMinorVersion()
  {
    return 1;
  }



  /**
   * Gets information about the possible properties for this driver.
   * <P>
   * The <code>getPropertyInfo</code> method is intended to allow a generic GUI
   * tool to discover what properties it should prompt a human for in order to
   * get enough information to connect to a database. Note that depending on the
   * values the human has supplied so far, additional values may become
   * necessary, so it may be necessary to iterate though several calls to the
   * <code>getPropertyInfo</code> method.
   * 
   * @param url
   *          the URL of the database to which to connect
   * @param info
   *          a proposed list of tag/value pairs that will be sent on connect
   *          open
   * @return an array of <code>DriverPropertyInfo</code> objects describing
   *         possible properties. This array may be an empty array if no
   *         properties are required.
   * @see java.sql.Driver#getPropertyInfo(java.lang.String,
   *      java.util.Properties)
   */
  public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
  {
    return new DriverPropertyInfo[0];
  }



  /**
   * Reports whether this driver is a genuine JDBC Compliant<sup><font
   * size=-2>TM</font></sup> driver. A driver may only report <code>true</code>
   * here if it passes the JDBC compliance tests; otherwise it is required to
   * return <code>false</code>.
   * <P>
   * JDBC compliance requires full support for the JDBC API and full support for
   * SQL 92 Entry Level. It is expected that JDBC compliant drivers will be
   * available for all the major commercial databases.
   * <P>
   * This method is not intended to encourage the development of non-JDBC
   * compliant drivers, but is a recognition of the fact that some vendors are
   * interested in using the JDBC API and framework for lightweight databases
   * that do not support full database functionality, or for special databases
   * such as document information retrieval where a SQL implementation may not
   * be feasible.
   * 
   * @return <code>true</code> if this driver is JDBC Compliant;
   *         <code>false</code> otherwise
   * @see java.sql.Driver#jdbcCompliant()
   */
  public boolean jdbcCompliant()
  {
    return false;
  }

}
